﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace TaoBaoProject.Handlers.OtherHanders
{
    /// <summary>
    /// Summary description for GetRequiredFromFile
    /// </summary>
    public class GetRequiredFromFile : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string type = context.Request.Params["type"];
            string result = "";
            try
            {
                if (type == "r")
                {
                    result = HandlerRequiredItems(context);
                }
                else if (type == "s")
                {

                    result = HandlerDefineSearchItems(context);
                }
                if (result == "")
                {
                    result = "-1";
                }
                context.Response.Write(result);
            }
            catch (Exception ex)
            {

            }
        }
        private string HandlerRequiredItems(HttpContext context)
        {
            string sb = "";
            XmlNodeList lists = GetNodeListFromXML(context.Server.MapPath("~/Config/isRequiredItems.xml"), "Projects");
            for (int i = 0; i < lists.Count; i++)
            {
                if (lists[i].Attributes["isRequired"].Value == "true")
                {
                    sb += lists[i].Name + ";";
                }
            }
            if (sb != "")
            {
                sb = sb.Substring(0, sb.Length - 1);
            }
            return sb;
        }
        private string HandlerDefineSearchItems(HttpContext context)
        {
            string sb = "";
            XmlNodeList lists = GetNodeListFromXML(context.Server.MapPath("~/Config/DefineSearchItems.xml"), "Projects");
            for (int i = 0; i < lists.Count; i++)
            {
                if (lists[i].Attributes["isSearch"].Value == "true")
                {
                    sb += lists[i].Name + ";";
                }
            }
            if (sb != "")
            {
                sb = sb.Substring(0, sb.Length - 1);
            }
            return sb;

        }
        private XmlNodeList GetNodeListFromXML(string path, string rootItem)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(path);
            XmlNode root = xml.SelectSingleNode(rootItem);
            XmlNodeList lists = root.ChildNodes;

            return lists;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}